package com.kaikai.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.edu.client.VodClient;
import com.kaikai.edu.entity.EduVideo;
import com.kaikai.edu.mapper.EduVideoMapper;
import com.kaikai.edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    VodClient vodClient;
    @Override
    public void deleteCourseByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduVideo> videoList = this.list(wrapper);

        //用来存储sourceId集合
        List sourceIdList=new ArrayList();
        for(EduVideo video : videoList){
            String videoSourceId=video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                sourceIdList.add(videoSourceId);
            }
        }
        if(sourceIdList.size()>0){
            vodClient.removeVideos(sourceIdList);
        }
        //删除小节
        this.remove(wrapper);
    }

    /**
     * 通过id查询视频videoId
     * @param id
     * @return
     */
    @Override
    public String getVideoIdById(String id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("video_source_id");
        wrapper.eq("id", id);
        EduVideo video = this.getOne(wrapper);
        return video.getVideoSourceId();
    }

}
